package kr.co.example.newgugudan;

import java.util.ArrayList;
import java.util.Random;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.style.SuperscriptSpan;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ProMental extends Activity {
	InputMethodManager mbn; // 보드
	TextView l_Value; // 계산
	TextView r_Value;
	TextView oper_Value;
	EditText result;
	
	int number; // 랜덤값
	int number1;

	int num; // o x의 결과값
	int num2;
	
	Handler mHandler;
	int value = 60;
	TextView texter;
	TextView result00;
	TextView result01;
	Random r = new Random();
	int mEllapse = 60;
	ProgressBar progressBar;

	WordDBHelper mHelper;
	EditText mText;
	SQLiteDatabase db;
	String a = null;
	String b = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.promentalgame);

		l_Value = (TextView) findViewById(R.id.prox);
		r_Value = (TextView) findViewById(R.id.proy);
		oper_Value = (TextView) findViewById(R.id.mult1);
		result = (EditText) findViewById(R.id.editText);
		mbn = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);// 아이디설정
		result00 = (TextView) findViewById(R.id.scv00);
		result01 = (TextView) findViewById(R.id.scv01);
		texter = (TextView) findViewById(R.id.textView3);

		progressBar = (ProgressBar) findViewById(R.id.progressBar1);

		result.setFocusable(false); // edit 사용 금지
		result.setClickable(false);
		result.setFocusableInTouchMode(false);

		number = r.nextInt(7); // 랜덤함수 설정
		l_Value.setText(number + 2 + "");
		number1 = r.nextInt(8);
		r_Value.setText(number + 1 + "");

		mHandler = new Handler() {
			public void handleMessage(Message msg) {
				value--;
				texter.setText("Value = " + value);
				if (value > 0) {
					mHandler.sendEmptyMessageDelayed(value, 1000);
					progressBar.incrementProgressBy(1);
				}else if (value ==0){
					/*new AlertDialog.Builder(ProMental.this)
					.setTitle("질문")
					.setMessage("종료하시겠습니까? 새로 하시겠습니까?")
					.setIcon(R.drawable.androboy)
					.setCancelable(false)
					.setPositiveButton("종료하기",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									mHelper = new WordDBHelper(ProMental.this);
									db = mHelper.getWritableDatabase();
									a = result00.getText().toString();
									b = result01.getText().toString();
									db.execSQL("INSERT INTO dic VALUES (null, '"+ a + "','" + b + "');");
									mHelper.close();
									finish();
								}
							})
					.setNegativeButton("새로하기",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									mHelper = new WordDBHelper(ProMental.this);
									db = mHelper.getWritableDatabase();
									a = result00.getText().toString();
									b = result01.getText().toString();
									db.execSQL("INSERT INTO dic VALUES (null, '"
											+ a + "','" + b + "');");
									mHelper.close();
									Intent intent = new Intent(ProMental.this, ProMental.class);
									startActivity(intent);
								}
							}).show();*/
					showDialog(0);
				}
			}
		};
		mHandler.sendEmptyMessage(value);
	}
	@Override
	@Deprecated
	protected void onPrepareDialog(int id, Dialog dialog) {
			super.onPrepareDialog(id, dialog);
			removeDialog(0);
	}

	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		if(id==0){
		dialog = new AlertDialog.Builder(ProMental.this)
		.setTitle("질문")
		.setMessage("종료하시겠습니까? 새로 하시겠습니까?")
		.setIcon(R.drawable.androboy)
		.setCancelable(false)
		.setPositiveButton("종료하기",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						mHelper = new WordDBHelper(ProMental.this);
						db = mHelper.getWritableDatabase();
						a = result00.getText().toString();
						b = result01.getText().toString();
						db.execSQL("INSERT INTO dic VALUES (null, '"+ a + "','" + b + "');");
						mHelper.close();
						Intent intent = new Intent(ProMental.this, MunuGugudan.class);
						startActivity(intent);
					}
				})
		.setNegativeButton("새로하기",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						mHelper = new WordDBHelper(ProMental.this);
						db = mHelper.getWritableDatabase();
						a = result00.getText().toString();
						b = result01.getText().toString();
						db.execSQL("INSERT INTO dic VALUES (null, '"
								+ a + "','" + b + "');");
						mHelper.close();
						finish();
						Intent intent = new Intent(ProMental.this, ProMental.class);
						startActivity(intent);
					}
				}).create();
		}
		return dialog;
	}

	public void mOnClick(View v) {
		mbn.hideSoftInputFromWindow(result.getWindowToken(), 0);
		// mPage1.setVisibility(View.INVISIBLE);
		// mPage2.setVisibility(View.INVISIBLE);

		switch (v.getId()) {

		case R.id.result_btn25:
			int a = 0;
			int b = 0;
			int c = 0;
			a = Integer.parseInt(l_Value.getText().toString());
			b = Integer.parseInt(r_Value.getText().toString());
			try {
				c = Integer.parseInt(result.getText().toString());
			} catch (Exception e) {
			}

			if (a * b == c) {
				number = r.nextInt(7);
				l_Value.setText(number + 2 + "");
				number = r.nextInt(8);
				r_Value.setText(number + 1 + "");
				result.setText("");
				num += 1;
				result00.setText(num + "");
			} else if (c == 0) {
				Toast.makeText(ProMental.this, "숫자를 입력해!", Toast.LENGTH_SHORT)
						.show();
			} else {
				number = r.nextInt(7);
				l_Value.setText(number + 2 + "");
				number = r.nextInt(8);
				r_Value.setText(number + 1 + "");
				result.setText("");
				num2 += 1;
				result01.setText(num2 + "");
			}
			break;

		case R.id.result_btn28:
			Editable edit = result.getText();
			int st = edit.length();
			if (st > 1) {
				edit.delete(st - 1, st);
				result.setText(edit);
			} else if (st <= 1) {
				result.setText("");
			}
			break;


		case R.id.button1_01:
			String text1 = result.getText().toString();
			result.setText(text1 + "1" + "");
			break;
		case R.id.button2_02:
			String text2 = result.getText().toString();
			result.setText(text2 + "2" + "");
			break;
		case R.id.button3_03:
			String text3 = result.getText().toString();
			result.setText(text3 + "3" + "");
			break;
		case R.id.button4_04:
			String text4 = result.getText().toString();
			result.setText(text4 + "4" + "");
			break;
		case R.id.button5_05:
			String text5 = result.getText().toString();
			result.setText(text5 + "5" + "");
			break;
		case R.id.button6_06:
			String text6 = result.getText().toString();
			result.setText(text6 + "6" + "");
			break;
		case R.id.button7_07:
			String text7 = result.getText().toString();
			result.setText(text7 + "7" + "");
			break;
		case R.id.button8_08:
			String text8 = result.getText().toString();
			result.setText(text8 + "8" + "");
			break;
		case R.id.button9_09:
			String text9 = result.getText().toString();
			result.setText(text9 + "9" + "");
			break;
		case R.id.button10_10:
			String text10 = result.getText().toString();
			result.setText(text10 + "0" + "");
			break;

		}
	}

}
